package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.InsuranceStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhInsuranceCompulsoryMapper;
import com.zjdiepu.www.dao.dh.FkInsureListMapper;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceCompulsoryService;
import com.zjdiepu.www.model.dh.dto.DhInsuranceCompulsory;
import com.zjdiepu.www.model.dh.dto.FkInsureList;
import com.zjdiepu.www.model.dh.ext.DhInsuranceCompulsoryExt;

/**
 * 保险_交强险Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月18日 10时52分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhInsuranceCompulsoryServiceImpl extends BaseService implements IDhInsuranceCompulsoryService {
	public static final Logger logger = LoggerFactory.getLogger(DhInsuranceCompulsoryServiceImpl.class);

	@Autowired
	private DhInsuranceCompulsoryMapper dhInsuranceCompulsoryMapper;
	
	@Autowired
	private FkInsureListMapper fkInsureListMapper;
	
	@Override
	public ApiResponse saveInsurance(
			DhInsuranceCompulsory dhInsuranceCompulsory,
			AbstractSysUser currentUser) {
		String id = dhInsuranceCompulsory.getId();
		if(StringUtils.isBlank(id)) {
			dhInsuranceCompulsory.initInsertData(currentUser.getId(), currentUser.getName());
			dhInsuranceCompulsoryMapper.insert(dhInsuranceCompulsory);
			FkInsureList fkInsureList=new FkInsureList();
			fkInsureList.setId(dhInsuranceCompulsory.getInsureListId());
			fkInsureList.setInsuranceStatus(InsuranceStatus.YI_BAO.getCode());
			fkInsureListMapper.updateByPrimaryKeySelective(fkInsureList);
		} else {
			dhInsuranceCompulsory.initUpdateData(currentUser.getId(), currentUser.getName());
			dhInsuranceCompulsoryMapper.updateByPrimaryKeySelective(dhInsuranceCompulsory);
		}
		return ApiResponse.success(dhInsuranceCompulsory.getId());
	}
	
	@Override
	public void saveUpdateAndDelete(DhInsuranceCompulsory dhInsuranceCompulsory, OperateType operType) {
		switch (operType) {
		case add:
			dhInsuranceCompulsory.initInsertData(dhInsuranceCompulsory.getModifier(), dhInsuranceCompulsory.getModifierName());
			dhInsuranceCompulsoryMapper.insert(dhInsuranceCompulsory);
			break;
		case edit:
			dhInsuranceCompulsory.initUpdateData(dhInsuranceCompulsory.getModifier(), dhInsuranceCompulsory.getModifierName());
			// 影响的记录数
			int updateCount = dhInsuranceCompulsoryMapper.updateByPrimaryKeySelective(dhInsuranceCompulsory);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhInsuranceCompulsory.initDeleteData(dhInsuranceCompulsory.getModifier(), dhInsuranceCompulsory.getModifierName());
			// 影响的记录数
			int deleteCount = dhInsuranceCompulsoryMapper.updateByPrimaryKeySelective(dhInsuranceCompulsory);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhInsuranceCompulsory> selectByExt(DhInsuranceCompulsoryExt dhInsuranceCompulsoryExt) {
		return dhInsuranceCompulsoryMapper.selectByExt(dhInsuranceCompulsoryExt);
	}
	
	@Override
	public int countByExt(DhInsuranceCompulsoryExt dhInsuranceCompulsoryExt){
		return dhInsuranceCompulsoryMapper.countByExt(dhInsuranceCompulsoryExt);
	}
	
	@Override
	public DhInsuranceCompulsory selectById(String id) {
		return dhInsuranceCompulsoryMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhInsuranceCompulsoryMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhInsuranceCompulsory dhInsuranceCompulsory) {
		return dhInsuranceCompulsoryMapper.countRefined(dhInsuranceCompulsory);
	}

	@Override
	public DhInsuranceCompulsory selectRefined(DhInsuranceCompulsory dhInsuranceCompulsory) {
		return dhInsuranceCompulsoryMapper.selectRefined(dhInsuranceCompulsory);
	}
	
	@Override
	public int deletedRefined(DhInsuranceCompulsory dhInsuranceCompulsory) {
		return dhInsuranceCompulsoryMapper.deletedRefined(dhInsuranceCompulsory);
	}

	@Override
	public boolean repeat(DhInsuranceCompulsory dhInsuranceCompulsory) {
		int count = countRefined(dhInsuranceCompulsory);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhInsuranceCompulsory.getId())) {
			return true;
		}
		DhInsuranceCompulsory dbItem = selectRefined(dhInsuranceCompulsory);
		if(null == dbItem) {
			return false;
		}
		return !dhInsuranceCompulsory.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhInsuranceCompulsoryMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<DhInsuranceCompulsory> dhInsuranceCompulsorys) {
		return dhInsuranceCompulsoryMapper.insertBatch(dhInsuranceCompulsorys);
	}
}
